//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-1999  Microsoft Corporation

Module Name:

atcmd.h

Abstract:


Notes:


--*/
/*
------------------------------------------------------------------------------
    Revision History
    Date                Author            Activity ID            Activity Headline
    2007-02-08          qinxuetao         CEDB200043651          Update ril driver for network and data service
------------------------------------------------------------------------------
*/
#pragma once


//
// Forward declarations
//
class CRilHandle;
enum APIID;


//
// Globals
//
extern CPriorityQueue<CCommand, 40>* g_pCmdQ;
extern CQueue<CResponse, 10>* g_pRspQ;
#if defined(OEM1_DRIVER) || defined(OEM2_DRIVER) || defined(EMP_DRIVER) || defined(WAVECOM_DRIVER)
extern DWORD g_dwUnlocked;
// theoretically, on boot we should see a max of two toolkit notifications.
// 1 for a setup menu and 1 for a pro-active cmd.
#define MAX_TOOLKITNOTIFS 3
extern CResponse* g_CachedToolKitNotifs[MAX_TOOLKITNOTIFS];
#endif

#if defined(OEM1_GPRS_DEACT) || defined(EMP_DRIVER)
extern CRITICAL_SECTION g_csGPRSDeactLock;
extern bool g_fDeactNotificationRcvd;
extern bool g_fDeactResponseRcvd;
extern DWORD g_LastDeactNotificationRcvd;
// The constant below is part of a heuristic used in the RIL driver which does not track calls.
// The value of this constant helps rule out ME DEACT notifications from the distant past in
// relation to the current deactivation session.
#define STALE_MEDEACT_THRESHOLD 5000 // in ms
#endif

//
// Structures
//
struct REQUEUE_THREAD_DATA {
    CCommand* pCmd;
    DWORD dwDelay;
};


//
// Thread procedures
//
DWORD WINAPI CmdThreadProc(LPVOID lpParameter);
DWORD WINAPI ReadThreadProc(LPVOID lpParameter);

//
// Routines
//
BOOL QueueCmd(CRilInstanceHandle* const pHandle, const LPCSTR szCmd, const DWORD dwOptions, const APIID apiid,
              const PFN_CMD_PARSE pfnParse, CNotificationData* const pnd, HRESULT& rhrCmdID);
BOOL QueueMultipartCmd(CRilInstanceHandle* const pHandle, const LPCSTR szCmd, const LPCSTR szCmdPart2, const DWORD dwOptions, const APIID apiid,
              const PFN_CMD_PARSE pfnParse, CNotificationData* const pnd, HRESULT& rhrCmdID);
BOOL QueueCmdWithTimeout(CRilInstanceHandle* const pHandle, const LPCSTR szCmd, const LPCSTR szCmdPart2, const DWORD dwOptions, const APIID apiid,
              const PFN_CMD_PARSE pfnParse, CNotificationData* const pnd, HRESULT& rhrCmdID, DWORD dwTimeout, DWORD dwRetries);
BOOL QueueCmdIgnoreRsp(const APIID apiid, const LPCSTR szCmd, const DWORD dwOptions, DWORD dwTimeout, const PFN_CMD_PARSE pfnParse, CNotificationData* const pnd, DWORD dwRetries, DWORD dwRetriesOnError, DWORD dwRetryOnErrorDelay);
BOOL QueueCmdIgnoreRspWithData(const APIID apiid, const LPCSTR szCmd, const DWORD dwOptions, DWORD dwTimeout, const PFN_CMD_PARSE pfnParse, CNotificationData* const pnd, DWORD dwRetries, DWORD dwRetriesOnError, DWORD dwRetryOnErrorDelay, const PFN_CMD_PARSE_DATA pfnParseWithData, const PVOID pParseData);
BOOL RequeueCmdWithDelay(CCommand* pCmd, DWORD dwDelay);
BOOL HandleRsp(CRilHandle* pDevice, CCommand* pCmd, CResponse* pRsp);

/*
 *  framework to support synchronous AT command execution
 */
class ATCmd
{
public:
    ATCmd();
    virtual ~ATCmd();

    virtual void Dump() const;

protected:

    HRESULT Execute( const char *cmd );

    virtual HRESULT Parse( LPCSTR szRsp ) =0;

private:

    ATCmd( const ATCmd& ref );
    ATCmd& operator=( const ATCmd& ref );

    mutable CRITICAL_SECTION m_cs;
    HANDLE m_hEvent;
    HRESULT m_hr;
    BOOL m_fInProgress;

    HRESULT Initialize();

    static HRESULT s_ParseFunc( LPCSTR szRsp, void*& pBlob, UINT& cbBlob, PVOID& pfnParseData );

    HRESULT ParseFunc( LPCSTR szRsp );
};
